package com.chixing.ih.module.programmer.controller;

import com.chixing.ih.json.ResponseData;
import com.chixing.ih.module.programmer.pojo.PersonalProject;
import com.chixing.ih.module.programmer.pojo.dto.PersonalProjectDTO;
import com.chixing.ih.module.programmer.service.IPersonalProjectService;
import com.chixing.ih.module.programmer.service.IProgrammerService;
import com.chixing.ih.util.RequestIdUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @version 1.0
 * @Author MasterYan
 * @Date 2020/10/5
 * Time: 17:52
 */
@RestController
@RequestMapping("/personalProject")
@Api(tags = "程序员模块项目相关Api")
public class PersonalProjectController {

    @Autowired
    IPersonalProjectService personalProjectService;

    @Autowired
    IProgrammerService programmerService;

    /**
     * desc:批量保存项目接口
     *
     * @param personalProjects
     * @return
     */
    @ApiOperation(value = "批量保存项目接口", notes = "批量保存项目")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "项目id", required = true, dataType = "long"),
            @ApiImplicitParam(name = "projectTitle", value = "项目标题", dataType = "string"),
            @ApiImplicitParam(name = "projectUrl", value = "项目网站", dataType = "string"),
            @ApiImplicitParam(name = "projectDesc", value = "项目描述", dataType = "string")

    })
    @ApiResponses({
            @ApiResponse(code = 201, message = "保存成功"),
            @ApiResponse(code = 400, message = "保存失败"),
    })

    @PutMapping("/personalProject")
    public Map<String, Object> updateListPerPros(@RequestBody List<PersonalProject> personalProjects, HttpServletRequest request) {
        Long myId = RequestIdUtil.getMyId(request);
        boolean flag = personalProjectService.updateList(personalProjects);
        if (flag) {
            programmerService.updateProgrammerTime(myId);
            return ResponseData.REP_CREATED_SUCCESS();
        } else
            return ResponseData.REP_INVALID_ERROR();


    }

    /**
     * desc:查询项目接口
     *
     * @return
     */
    @ApiOperation(value = "查询项目接口", notes = "查询所有项目")

    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "程序员id", required = true, dataType = "long"),

    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 400, message = "查询失败"),
    })


    @GetMapping("/personalProject/{userId}")
    public Map<String, Object> listPerPros(@PathVariable("userId") long userId,HttpServletRequest request) {
        List<PersonalProjectDTO> personalProjectDTOS = personalProjectService.listAll(userId);
        return ResponseData.REP_GET_SUCCESS(personalProjectDTOS);


    }

    /**
     * desc:获取新增项目id接口
     *
     * @return
     */
    @ApiOperation(value = "获取新增项目id接口", notes = "获取新增项目id")
    @ApiResponses({
            @ApiResponse(code = 200, message = "新增成功"),
            @ApiResponse(code = 400, message = "新增失败"),
    })

    @PostMapping("/personalProject")
    public Map<String, Object> savePerPro(HttpServletRequest request) {
        Long myId = RequestIdUtil.getMyId(request);
        PersonalProject personalProject = new PersonalProject();
        personalProject.setUserId(myId);
        PersonalProject personalProject1 = personalProjectService.savePersonalProject(personalProject);
        if (personalProject1 != null) {
            programmerService.updateProgrammerTime(myId);
            return ResponseData.REP_GET_SUCCESS(personalProject1);
        } else
            return ResponseData.REP_INVALID_ERROR();


    }


    /**
     * desc:删除一条项目接口
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除一条项目接口", notes = "批量一条项目")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "项目id", required = true, dataType = "long"),

    })
    @ApiResponses({
            @ApiResponse(code = 201, message = "删除成功"),
            @ApiResponse(code = 400, message = "删除失败"),
    })

    @DeleteMapping("/personalProject/{id}")
    public Map<String, Object> deletePerPro(@PathVariable("id") long id, HttpServletRequest request) {
        Long myId = RequestIdUtil.getMyId(request);
        boolean flag = personalProjectService.deletePersonalProject(id);
        if (flag) {
            programmerService.updateProgrammerTime(myId);
            return ResponseData.REP_CREATED_SUCCESS();
        } else
            return ResponseData.REP_INVALID_ERROR();

    }


}
